<template>
  <div class="register-page">
    <div class="register-container">
      <div class="register-left">
        <div class="welcome-text">
          <h1>加入我们</h1>
          <p>创建您的账户，享受购物乐趣</p>
          <div class="features">
            <div class="feature-item">
              <el-icon><Avatar /></el-icon>
              <span>专属会员</span>
            </div>
            <div class="feature-item">
              <el-icon><Discount /></el-icon>
              <span>专属优惠</span>
            </div>
            <div class="feature-item">
              <el-icon><Promotion /></el-icon>
              <span>积分奖励</span>
            </div>
          </div>
        </div>
      </div>
      
      <div class="register-right">
        <el-card class="register-card" shadow="always">
          <template #header>
            <div class="card-header">
              <h2>创建账户</h2>
              <p class="subtitle">请填写您的注册信息</p>
            </div>
          </template>
          
          <el-form :model="registerForm" :rules="rules" ref="registerFormRef" label-width="0">
            <el-form-item prop="username">
              <el-input 
                v-model="registerForm.username" 
                placeholder="用户名（3-20个字符）"
                size="large"
                prefix-icon="User"
                clearable
              />
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input 
                v-model="registerForm.password" 
                type="password" 
                placeholder="密码（6-20个字符）"
                size="large"
                prefix-icon="Lock"
                show-password
              />
            </el-form-item>
            
            <el-form-item prop="confirmPassword">
              <el-input 
                v-model="registerForm.confirmPassword" 
                type="password" 
                placeholder="确认密码"
                size="large"
                prefix-icon="Lock"
                show-password
                @keyup.enter="handleRegister"
              />
            </el-form-item>
            
            <el-form-item prop="email">
              <el-input 
                v-model="registerForm.email" 
                placeholder="邮箱地址"
                size="large"
                prefix-icon="Message"
                clearable
              />
            </el-form-item>
            
            <el-form-item>
              <el-button 
                type="primary" 
                @click="handleRegister" 
                :loading="loading" 
                size="large"
                class="register-button"
              >
                {{ loading ? '注册中...' : '立即注册' }}
              </el-button>
            </el-form-item>
          </el-form>
          
          <div class="login-link">
            <span>已有账号？</span>
            <el-link type="primary" @click="$router.push('/login')" :underline="false">
              立即登录
            </el-link>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import { ElMessage } from 'element-plus'
import { User, Lock, Message, Avatar, Discount, Promotion } from '@element-plus/icons-vue'

const router = useRouter()
const authStore = useAuthStore()

const registerFormRef = ref(null)
const loading = ref(false)

const registerForm = ref({
  username: '',
  password: '',
  confirmPassword: '',
  email: ''
})

const validateConfirmPassword = (rule, value, callback) => {
  if (value !== registerForm.value.password) {
    callback(new Error('两次输入的密码不一致'))
  } else {
    callback()
  }
}

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
    { validator: validateConfirmPassword, trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' },
    { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
  ]
}

const handleRegister = async () => {
  if (!registerFormRef.value) return
  
  await registerFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      
      try {
        const result = await authStore.register(
          registerForm.value.username,
          registerForm.value.password,
          registerForm.value.email
        )
        
        if (result.success) {
          ElMessage.success('注册成功！正在跳转到登录页面...')
          
          setTimeout(() => {
            router.push('/login')
          }, 1500)
        } else {
          ElMessage.error(result.message || '注册失败')
        }
      } catch (error) {
        console.error('注册失败:', error)
        ElMessage.error('注册失败，请检查网络连接')
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.register-page {
  min-height: 100vh;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
}

.register-container {
  display: flex;
  width: 100%;
  max-width: 1200px;
  min-height: 600px;
  background: white;
  border-radius: 0;
  overflow: hidden;
  box-shadow: none;
  border: 1px solid #e8e8e8;
}

.register-left {
  flex: 1;
  background: #1a1a1a;
  padding: 80px 60px;
  display: flex;
  align-items: center;
  color: white;
  min-height: 600px;
}

.welcome-text h1 {
  font-size: 42px;
  margin: 0 0 20px 0;
  font-weight: 300;
  letter-spacing: 2px;
}

.welcome-text p {
  font-size: 18px;
  margin-bottom: 50px;
  opacity: 0.9;
}

.features {
  display: flex;
  flex-direction: column;
  gap: 30px;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 15px;
  font-size: 16px;
}

.feature-item .el-icon {
  font-size: 24px;
}

.register-right {
  flex: 1;
  padding: 80px 60px;
  display: flex;
  align-items: center;
  min-height: 600px;
}

.register-card {
  width: 100%;
  border: none;
  box-shadow: none;
}

.card-header {
  text-align: center;
  margin-bottom: 30px;
}

.card-header h2 {
  margin: 0 0 10px 0;
  color: #303133;
  font-size: 28px;
  font-weight: bold;
}

.subtitle {
  color: #909399;
  font-size: 14px;
  margin: 0;
}

.register-button {
  width: 100%;
  height: 50px;
  font-size: 16px;
  font-weight: bold;
  margin-top: 10px;
}

.login-link {
  text-align: center;
  margin-top: 20px;
  color: #606266;
  font-size: 14px;
}

.login-link .el-link {
  margin-left: 5px;
  font-weight: 500;
}

@media (max-width: 768px) {
  .register-container {
    flex-direction: column;
  }
  
  .register-left {
    padding: 40px 30px;
  }
  
  .welcome-text h1 {
    font-size: 32px;
  }
  
  .register-right {
    padding: 40px 30px;
  }
}
</style>

